package com.yunhe.authority.service.company.impl;

import com.yunhe.authority.domain.authority.Firm;
import com.yunhe.authority.domain.authority.RealStation;
import com.yunhe.authority.domain.company.Company;
import com.yunhe.authority.domain.company.CompanyTree;
import com.yunhe.authority.domain.company.ProjectCompanyTree;
import com.yunhe.authority.service.authority.FirmService;
import com.yunhe.authority.service.authority.RealStationService;
import com.yunhe.authority.service.authority.StationService;
import com.yunhe.authority.service.company.CompanyTreeService;
import com.yunhe.authority.domain.authority.Station;
import com.yunhe.authority.service.company.CompanyService;
import com.yunhe.common.filter.ServerContextHolder;
import com.yunhe.common.util.DateTimeUtils;
import com.yunhe.common.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class CompanyTreeServiceImpl implements CompanyTreeService {

    @Autowired
    private FirmService firmService;
    @Autowired
    private StationService stationService;
    @Autowired
    private CompanyService companyService;

    @Autowired
    private RealStationService realStationService;


    private CompanyTree buildTree(Long id, String title, Long code, int level, Object obj, String type) {
        CompanyTree companyTree = new CompanyTree();
        companyTree.setTitle(title);
        companyTree.setId(id);
        companyTree.setCode(code);
        companyTree.setLevel(level);
        companyTree.setObject(obj);
        companyTree.setType(type);
        return companyTree;
    }

    private ProjectCompanyTree buildTree(Long id, String title, String code, Long parentId,String iconFlag, int level, String type) {
        ProjectCompanyTree projectTree = new ProjectCompanyTree();
        projectTree.setTitle(title);
        projectTree.setId(id);
        projectTree.setCode(code);
        projectTree.setParentId(parentId);
        projectTree.setIconFlag(iconFlag);
        projectTree.setLevel(level);
        projectTree.setType(type);
        return projectTree;
    }

    @Override
    public Iterable<CompanyTree> getTree(Long firmCode) {
        //组建第一层树形
        Firm firm = firmService.findByCode(firmCode);
        CompanyTree firmTree = buildTree(firm.getId(), firm.getTitle(), firmCode, 1, firm, "firm");
        Iterable<Station> stations = stationService.findByFirmId(firm.getId());
        if (stations != null) {
            for (Station station : stations) {
                CompanyTree companyTree = buildTree(station.getId(), station.getTitle(), station.getCode(), 2, station, "station");
                firmTree.addChild(companyTree);
                List<Company> companys = companyService.findByStationId(station.getId());
                if (companys != null) {
                    for (Company company : companys) {
                        companyTree.addChild(buildTree(company.getId(), company.getTitle(), company.getCode(), 3, company, "company"));
                    }
                }
            }
        }
        return Collections.singletonList(firmTree);
    }

    @Override
    public ProjectCompanyTree getTree(String type) {
        ProjectCompanyTree projectCompanyTree = new ProjectCompanyTree();
        //获取项目id
        String projectCode = ServerContextHolder.getContext().getProjectCode();
        if(StringUtil.isEmpty(projectCode)){
            return null;
        }
        Long stationId = Long.valueOf(projectCode);
        Firm firm = firmService.findByStationId(stationId);
        if(firm !=null){
            projectCompanyTree = buildTree(firm.getId(), firm.getEnterpriseFullName(), firm.getName(), null,null, 1, "firm");
            Station station = stationService.findById(stationId);
            if(station != null){
                ProjectCompanyTree stationTree = buildTree(station.getId(), station.getEnterpriseFullName(), station.getName(), firm.getId(), null, 2, "station");
                Iterable<RealStation> realStations = realStationService.findRealStationByStationId(stationId);
                if(realStations != null){
                    for (RealStation realStation : realStations) {
                        ProjectCompanyTree realStationTree = buildTree(realStation.getId(), StringUtil.isEmpty(realStation.getAbbreviationName()) ?
                                realStation.getEnterpriseFullName() : realStation.getAbbreviationName(), realStation.getName(), station.getId(), null, 3, "realStation");
                        //通过真实园区查询企业
                        String iconFlag = "0";
                        if(StringUtil.isEmpty(type)){
                            Iterable<Company> companies = companyService.findByRealStationId(realStation.getId());
                            if(companies != null){
                                for (Company company : companies) {
                                    //company.getStatus();
                                    if("0".equals(company.getCompanyStatus())){
                                        iconFlag = "1";
                                        String unifiedSocialCreditCode = company.getUnifiedSocialCreditCode();
                                        if(StringUtil.isEmpty(unifiedSocialCreditCode)){
                                            String creatTime = company.getCreatTime();
                                            Long timestamp = DateTimeUtils.getTimestamp(creatTime, null);
                                            long currentTime = System.currentTimeMillis();
                                            if((currentTime-timestamp) >90*24*60*60*1000L){
                                                iconFlag = "2";
                                            }
                                        }
                                    }else {
                                        iconFlag = "0";
                                    }
                                    ProjectCompanyTree companyTree = buildTree(company.getId(),company.getTitle() , company.getName(), station.getId(), iconFlag, 4, "company");
                                    realStationTree.addChild(companyTree);
                                }
                            }
                        }else {
                            Iterable<Company> companies = companyService.findByRealStationId(realStation.getId(),type);
                            if(companies != null){
                                for (Company company : companies) {
                                    if("0".equals(company.getCompanyStatus())){
                                        iconFlag = "1";
                                        String unifiedSocialCreditCode = company.getUnifiedSocialCreditCode();
                                        if(StringUtil.isEmpty(unifiedSocialCreditCode)){
                                            String creatTime = company.getCreatTime();
                                            Long timestamp = DateTimeUtils.getTimestamp(creatTime, null);
                                            long currentTime = System.currentTimeMillis();
                                            if((currentTime-timestamp) >90*24*60*60*1000L){
                                                iconFlag = "2";
                                            }
                                        }
                                    }else {
                                        iconFlag = "0";
                                    }
                                    ProjectCompanyTree companyTree = buildTree(company.getId(),company.getTitle() , company.getName(), realStation.getId(), iconFlag, 4, "company");
                                    realStationTree.addChild(companyTree);
                                }
                            }
                        }
                        stationTree.addChild(realStationTree);
                    }
                }
                projectCompanyTree.addChild(stationTree);
            }
        }
        return projectCompanyTree;
    }
}
